﻿using PEPe.DataAccessModel;
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Data.Objects;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PEPe.ADO
{
    public class MedicamentoADO
    {

        /// <summary>
        /// Consulta um determinado registro
        /// </summary>
        /// <param name="medicamentoId"></param>
        /// <returns></returns>
        public Medicamento Consulta(int medicamentoID)
        {
            using (var db = new PEPeEntities())
            {
                return db.Medicamento.AsQueryable().Where(x => x.Id == medicamentoID ).FirstOrDefault();
            }

        }

        public Medicamento ConsultaPorNome(string nome)
        {
            using (var db = new PEPeEntities())
            {
                return db.Medicamento.AsQueryable().Where(x => x.Descricao == nome).FirstOrDefault();
            }

        }


        /// <summary>
        /// Consulta dados paginados no banco
        /// </summary>
        /// <param name="searsh"></param>
        /// <param name="skip"></param>
        /// <param name="take"></param>
        /// <returns></returns>
        public IEnumerable<Medicamento> ListarMedicamentoPaginado(string searsh, int skip, int take)
        {
            IEnumerable<Medicamento> lista = null;
            using (var db = new PEPeEntities())
            {

                if (string.IsNullOrEmpty(searsh))
                    lista = db.Medicamento.OrderBy(x => x.Descricao);
                else
                    lista = (from item in db.Medicamento
                             where item.Descricao.ToUpper().Contains(searsh.ToUpper())
                             orderby item.Descricao
                             select item);

                return lista.Skip(skip).Take(take).ToList();
            }

        }


        /// <summary>
        /// Retorna total de registros
        /// </summary>
        /// <returns></returns>
        public int TotalRegistros(string search)
        {
            using (var db = new PEPeEntities())
            {
                int total = 0;

                if (string.IsNullOrEmpty(search))
                    total = db.Medicamento.Count();
                else
                    db.Medicamento.Where(x => x.Descricao.ToUpper().Contains(search.ToUpper())).Count();


                return total;
            }

        }

        /// <summary>
        /// Grava 
        /// </summary>
        /// <param name="medicamento"></param>
        public void Salvar(Medicamento medicamento)
        {
            using (var db = new PEPeEntities())
            {
                if (medicamento.Id == 0)
                {
                    db.Medicamento.Add(medicamento);
                }
                else
                {
                    //Edita Agenda
                    Medicamento upd = db.Medicamento.Find(medicamento.Id);
                    db.Entry(upd).CurrentValues.SetValues(medicamento);
                }
                db.SaveChanges();
            }
        }


        /// <summary>
        /// Excluir
        /// </summary>
        /// <param name="Id"></param>
        public void Excluir(int Id)
        {
            using (var db = new PEPeEntities())
            {
                Medicamento medicamento = db.Medicamento.Find(Id);

                db.Medicamento.Remove(medicamento);
                db.SaveChanges();
            }
        }

    }
}
